/*
 ********************************************************************************
 * @FileName  : htp_svc.c  Health T
 * @CreateDate: 2021-8-23
 * @Author    : GaoQiu
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 ********************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */

#include "stack/host/att_uuid.h"
#include "stack/host/att_api.h"

#include "utils/byte_stream.h"

#include "svc_handle.h"

/*! HTS service declaration */
static const uint8_t  htsSvc[] = { UINT16_TO_BYTES(ATT_UUID_HEALTH_THERM_SERVICE) };
static const uint16_t htsSvcLen = sizeof(htsSvc);


/*! Temp Measurement characteristic */
static const uint8_t tempMeasProp[] = { ATT_PROP_INDICATE,
		                                UINT16_TO_BYTES(HTS_TEMP_MEAS_VAL_HDL),
		                                UINT16_TO_BYTES(ATT_UUID_TEMP_MEAS)};
static const uint16_t tempMeasPropLen = sizeof(tempMeasProp);

uint8_t tempMeasVal[] = {0x00, UINT32_TO_BYTES(0xFF000113)};
uint16_t tempMeasValLen = sizeof(tempMeasVal);

uint8_t tempMeasValCccd[] = {UINT16_TO_BYTES(0x0000)};
const uint16_t tempMeasValCccdLen = sizeof(tempMeasValCccd);

AttsAttr_t htsAttrs[] =
{
	/* HTS Service */
	{
		primSvcUuid,
		(uint8_t *)htsSvc,
		(uint16_t *)&htsSvcLen,
		sizeof(htsSvc),
		0,
		ATTS_PERMIT_READ,
	},

	{
		charDecUuid,
		(uint8_t *)tempMeasProp,
		(uint16_t *)&tempMeasPropLen,
		sizeof(tempMeasProp),
		0,
		ATTS_PERMIT_READ,
	},
	{
		htsTempMeasUuid,
		tempMeasVal,
		&tempMeasValLen,
		sizeof(tempMeasVal),
		0,
		ATTS_PERMIT_READ,
	},
	{
		clientCharCfgUuid,
		tempMeasValCccd,
		(uint16_t *)&tempMeasValCccdLen,
		sizeof(tempMeasValCccd),
		ATTS_SET_CCC,
		ATTS_PERMIT_READ|ATTS_PERMIT_WRITE,
	},
};

AttsGroup_t htsGrp =
{
	NULL,
	(AttsAttr_t *)htsAttrs,
	NULL,
	NULL,
	HTS_SVC_START_HDL,
	HTS_SVC_END_HDL,
};


uint8_t HTS_TempUpd(uint16_t connHandle)
{
	tempMeasVal[1]++;
	if(tempMeasVal[1] >= 255){
		tempMeasVal[1] = 20;
	}
	return ATTS_ValueIndicate(connHandle, HTS_TEMP_MEAS_VAL_HDL, tempMeasVal, tempMeasValLen);
}
